package com.ruoyi.common.utils;

import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.WxMoudle.Bases.WeixinConfig;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletConfig;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*公众号推送消息，每个模板传递参数不一致，具体对照公众号进行进行填写*/

public class PushMsg {


    public static void pushWxUser(List<String> openIds, BigDecimal money,String shopName,String orderNum,Long shopId) {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(WeixinConfig.APPID);
        wxStorage.setSecret(WeixinConfig.APPSECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        //2,消息模板
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(WeixinConfig.PUSH_MSG_TEMPLATE);
        templateMessage.setUrl(WeixinConfig.NOW_URL+"/wxMoudle/shop/order_manage.html?shopId="+shopId);

        //3.填写模板参数
        List<WxMpTemplateData> wxMpTemplateData = new ArrayList<>();
        wxMpTemplateData.add(new WxMpTemplateData("first", "您有一笔收款到账"));
        wxMpTemplateData.add(new WxMpTemplateData("keyword1",shopName));    //店名
        wxMpTemplateData.add(new WxMpTemplateData("keyword2", money.toString()));   //收款金额
        wxMpTemplateData.add(new WxMpTemplateData("keyword3", DateUtils.getTime()));  // 收款时间
        wxMpTemplateData.add(new WxMpTemplateData("keyword4", orderNum));   //订单号
        wxMpTemplateData.add(new WxMpTemplateData("remark", "详情请前往门店账户余额查看"));
        templateMessage.setData(wxMpTemplateData);
        try {
            for (String openId : openIds) {
                templateMessage.setToUser(openId);//要推送的用户openid
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            }
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }


}